package com.example.springboot.controller;

import com.example.springboot.config.anotation.repeatsubimit.ExcludeForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 登录适配器
 *
 * @Author:ChenZhangKun
 * @Date: 2020/9/16 20:52
 */
@Controller
@Api(tags = "登录")
public class LoginController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/user/login")
    @ExcludeForm(timeOut = 1000)
    @ApiOperation(value = "登录")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map,
                        HttpSession session) throws InterruptedException {
        Thread.sleep(3000);
        if (!StringUtils.isEmpty(username) && Objects.equals("1234", password)) {
            // 登陆成功，为了防止表单重复提交，可以重定向
            // 设置session,防止直接访问
            session.setAttribute("loginUser", username);
            return "redirect:/main.html";
        } else {
            // 错误回显
            map.put("msg", "用户名密码错误");
            return "login";
        }
    }

    @GetMapping("/test")
    @ResponseBody

    public List<Map<String, Object>> fun_1() {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from bc_staff");
        return maps;
    }

    @PostMapping("/czk/login")
    @ApiOperation("防止重复提交")
    @ResponseBody
    @ExcludeForm(timeOut = 1000)
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password) {
        System.out.println("czk");
        return "czk";
    }
}
